<template>
  <div class="sms-Login">
    <div class="login-content">
      <div class="top">
        <img src="https://msmk2019.oss-cn-shanghai.aliyuncs.com/uploads/image/2019EMSgrHV5vZ1566894495.png" alt="">
      </div>
      <div :class="active!= 1 ? 'int_num':'int_num int_num_active'">
        <input
          class="iptuser"
          type="tel"
          placeholder="请输入手机号"
          maxlength="11"
          v-model="getsmscodeData.mobile"
          @focus="active=1"
          @blur="active=''"
        />
        <div class="getCAPTCHA" v-on:click="getsmsCode" v-if="!timebool">获取验证码</div>
        <div class="getCAPTCHA" v-if="timebool" style="color:#ccc;">获取验证码({{time}})</div>
      </div>
      <div :class="active!= 2 ? 'int_num':'int_num int_num_active'">
        <input
          class="iptsms"
          type="tel"
          placeholder="请输入短信验证码"
          v-model="getsmscodeData.smscode"
          @focus="active=2"
          @blur="active=''"
        />
      </div>
      <div class="others">
        <span class="forget">*未注册的手机号将自动注册</span>
        <span class="login_CAPTCHA" @click="$router.go(-1)">使用密码登录</span>
      </div>
    </div>
    <div class="login">
      <span @click="goLogin">登录</span>
    </div>
    <!-- <div class="otherLogin" v-if="showOther">
      <p>第三方登录</p>
      <div class="txLogin">
        <div class="wxLogin" @click="qqLogin(1)">
          <img src="@/assets/images/wechat.png" />
          <span>微信登录</span>
        </div>
        <div class="qqLogin" @click="qqLogin(2)">
          <img src="@/assets/images/QQ.png" alt />
          <span>QQ登录</span>
        </div>
      </div>
    </div> -->
  </div>
</template>
<script>
import {Login} from "../../request/api.js"
import {yanMA} from "../../request/api.js"
export default {
  name: "SmsLogin",
  data() {
    return {
      getsmscodeData: {
        mobile: "",
        sms_type: "login",
        smscode: ""
      },
      time: 60,
      timebool: false,
      type: "",
      active: "",
      showOther: false
    };
  },
  methods: {
    otherLogin(code, type) {
      this.$http
        .post(`/api/app/webOauths`, {
          code,
          type
        })
        .then(res => {
          // 判断是否有手机号
          if (res.mobile) {
            localStorage.setItem("adminToken", res.remember_token);
            localStorage.setItem("user_id", res.id);
            if (res.is_new == 1) {
              this.$store.commit("GETLOGINPARAMS", {
                mobile: this.getsmscodeData.mobile,
                sms_code: this.getsmscodeData.smscode
              });
              this.$router.push({
                name: "SetPass"
              });
              return;
            }
            this.$router.go(-1);
          } else {
            this.$router.push({
              name: "BindMobile",
              query: {
                qq_openid: res.qq_openid,
                wx_openid: res.wx_openid,
                code: code,
                type: type
              }
            });
          }
        });
    },
    getsmsCode() {
      let res =/^(?:(?:\+|00)86)?1[3-9]\d{9}$/
      if (!res.test(this.getsmscodeData.mobile)) {
       this.$toast("手机号码格式不正确");
        return;
      }
      // this.timebool = true;
      //     this.time = 60;
      //     let _this = this;
      //     var timer = setInterval(function() {
      //       _this.time--;
      //       if (_this.time <= 0) {
      //         _this.timebool = false;
      //         clearInterval(timer);
      //       }
      //     }, 1000);
          var obj= {
          mobile: this.getsmscodeData.mobile,
          sms_type: this.getsmscodeData.sms_type
        }
        yanMA(obj).then(() => {
          this.timebool = true;
          this.time = 60;
          let _this = this;
          var timer = setInterval(function() {
            _this.time--;
            if (_this.time <= 0) {
              _this.timebool = false;
              clearInterval(timer);
            }
          }, 1000);
        });
    },
    goLogin() {
      let res =/^(?:(?:\+|00)86)?1[3-9]\d{9}$/
      if (!res.test(this.passwordData.mobile)) {
        this.$toast("手机号码格式不正确");
        return;
      }
        var obj= {
          mobile: this.passwordData.mobile,
          password: this.passwordData.user_pass,
          type: 1
        }
        Login(obj).then(res => {
          console.log(res)
          localStorage.setItem("adminToken", res.data.remember_token);
          localStorage.setItem("user_id", res.data.id);
             if (res.is_new == 1) {
            this.$store.commit("GETLOGINPARAMS", {
              mobile: this.getsmscodeData.mobile,
              sms_code: this.getsmscodeData.smscode
            });
            this.$router.push({
              name: "SetPass"
            });
            return;
          }
            this.$router.push({
              name: "my"
            });
        });
    },
  },
  mounted() {
    let href = window.location.href;
    if (href.search("code") > 0) {
      href = href.split("code=")[1];
      if (localStorage.getItem("login_way") == 0) {
        href = href.slice(0, href.indexOf("&"));
      }
      this.type = localStorage.getItem("login_way");
      this.otherLogin(href, this.type);
    }
  }
};
</script>
<style scoped lang="scss">
.login-page {
  overflow: hidden;
}
.login-content {
  padding: 0 20px;
}
.top {
  img{
    width: 70%;
    display: block;
    margin: 50px auto;
  }
  .logo {
    display: block;
    width: 162px;
    margin-top: 80px;
  }
  h4 {
    font-size: 48px;
    font-family: PingFangSC-Semibold;
    font-weight: 600;
    color: rgba(89, 89, 89, 1);
    margin-top: 160px;
  }
}
input::-webkit-input-placeholder {
  /* placeholder颜色  */
  color: rgba(190, 190, 190, 1);
}
.int_num {
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #f1f1f1;
  position:relative;
  .getCAPTCHA{
      position:absolute;
      right:10px;
      top:20px;
      color:#eb6100;
  }
  input {
    box-sizing: border-box;
    padding: 30px 0;
    border: 0;
    width: 540px;
    height: 100%;
    font-size: 18px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    line-height: 40px;
    float: left;
    outline: none;
  }
}
.int_num_active {
  border-color:#eb6100;
}
.login {
  width: 80%;
  height: 40px;
  margin: 30px auto ;
  // height: 374px;
  background: url("../../assets/theme-img/login-btn.png");
  background-size: 100% 100%;
  font-size: 20px;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  text-align: center;
  color: rgba(255, 255, 255, 1);
  letter-spacing: 4px;
  span {
    display: inline-block;
    width: 100%;
    height: 40px;
    line-height: 40px;
  }
}
.others {
  width: 100%;
  margin-top: 30px;
  display: flex;
  justify-content: space-between;
  .forget {
    font-size: 18px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }
  .login_CAPTCHA {
    font-size: 18px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }
}
</style>
